package demo.practice.binary_tree;

import demo.practice.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Offer32_1_2 {

    public int[] levelOrder(TreeNode root) {
        if(root==null)
            return new int[0];
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        List<Integer> list = new ArrayList<>();
        while (queue.size() > 0) {
            TreeNode poll = queue.poll();
            list.add(poll.val);
            if (poll.left != null)
                queue.add(poll.left);
            if (poll.right != null)
                queue.add(poll.right);
        }
        int[] res = new int[list.size()];
        int count = 0;
        for (Integer i : list) {
            res[count++] = i;
        }
        return res;
    }


}
